package com.iot08.api.controller.base;

import com.iot08.api.vo.request.VersionVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @version 1.0
 * @Description: 控制器基类
 * @time: 2020/11/3 16:28
 * @author: Rock
 */
@Slf4j
public class BaseController {


    /**
     * @time: 2020/12/16 15:41
     * @author: Rock
     * @version 1.0
     */
    protected HttpServletRequest getRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * 获取当前请求ip地址
     *
     * @time: 2020/9/10 9:38
     * @author: Rock
     * @version 1.0
     */
    protected String getRemortIP() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (StringUtils.isEmpty(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取请求头中的uuids
     *
     * @time: 2020/12/16 15:42
     * @author: Rock
     * @version 1.0
     */
    protected String getUuid() {
        return getRequest().getHeader("uuid");
    }

    /**
     * 获取请求头中的version
     *
     * @param
     * @time: 2020/12/21 18:17
     * @author: Rock
     * @version 1.0
     */
    protected String getVersion() {
        return getRequest().getHeader("version");
    }

    /**
     * @description 获取请求接口的版本及系统信息
     * @author cwj
     * @param [req]
     * @return com.iot08.api.vo.request.VersionVo
     * @date @time 2025/3/8 16:16
     **/
    protected VersionVo obtainVersionInfo(HttpServletRequest req) {
        VersionVo versionVo = new VersionVo();
        //请求头数据解析处理
        versionVo.setUserAgent(req.getHeader("user-agent"));
        versionVo.setVersion(req.getHeader("version"));
        return versionVo;
    }

}
